package org.ioc.com.book7.controller;

import jakarta.servlet.http.HttpSession;
import org.ioc.com.book7.constant.Constants;
import org.ioc.com.book7.model.UserInfo;
import org.ioc.com.book7.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;
    //用户登录
    @RequestMapping("/login")
    public String login(String userName, String password, HttpSession session){
        //1.参数校验
        if(!StringUtils.hasLength(userName)||!StringUtils.hasLength(password)){
            return "用户名或密码为空";
        }
        //判断
        UserInfo userInfo=userInfoService.select(userName);
        if (userInfo==null){
            return "用户不存在";
        }
        if (!password.equals(userInfo.getPassword())){
            return "密码错误";
        }
        //存储session后续登录验证
        session.setAttribute(Constants.USER_SESSION_KEY,userInfo);
        return "";
    }

}
